package com.funtl.framework.web.action.purview;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.springframework.context.annotation.Scope;

import com.funtl.framework.java.abstracts.AbstractAction;
import com.funtl.framework.java.common.util.bean.DateUtil;
import com.funtl.framework.web.entity.Admin;
import com.funtl.framework.web.entity.PurviewColumn;
import com.funtl.framework.web.entity.PurviewGroup;
import com.funtl.framework.web.entity.PurviewManager;
import com.funtl.framework.web.entity.PurviewMenu;
import com.funtl.framework.web.service.purview.IPurviewColumnService;
import com.funtl.framework.web.service.purview.IPurviewGroupService;
import com.funtl.framework.web.service.purview.IPurviewManagerService;
import com.funtl.framework.web.service.purview.IPurviewMenuService;

/**
 * 权限管理
 * @author WMLI
 * Create on 2013-5-15
 */
@Scope("prototype")
public class PurviewGroupAction extends AbstractAction {
	private static final long serialVersionUID = -8258321024140886793L;
	private JSONObject result;
	private Map<String, Object> request;
	private Map<String, Object> session;
	
	private PurviewGroup purviewGroup;
	private PurviewManager purviewManager;
	private IPurviewGroupService purviewGroupService;
	private IPurviewManagerService purviewManagerService;
	private IPurviewColumnService purviewColumnService;
	private IPurviewMenuService purviewMenuService;
	
	//参数
	private String[] purviewMenus;	//权限菜单
	
	//page
	
	public String group_manager() {
		List<PurviewColumn> columns = purviewColumnService.getList(new PurviewColumn());
		List<PurviewMenu> menus = purviewMenuService.getList(new PurviewMenu());
		request.clear();
		request.put("columns", columns);
		request.put("menus", menus);
		return SUCCESS;
	}
	
	//action
	
	/**
	 * 查询管理组
	 * @return
	 * @throws Exception
	 */
	public String queryManager() throws Exception {
		List<PurviewManager> list = purviewManagerService.getList(new PurviewManager());
		jsonData.put("rows", list);
		result = JSONObject.fromObject(jsonData);
		return SUCCESS;
	}
	
	/**
	 * 新增管理组
	 * @return
	 * @throws Exception
	 */
	public String insertManager() throws Exception {
		Admin admin = (Admin) session.get("admin");
		purviewManager.setCreateDate(DateUtil.getCurrentDate());
		purviewManager.setCreateId(admin.getId());
		purviewManager.setCreateName(admin.getUserName());
		purviewManagerService.insert(purviewManager);
		result = JSONObject.fromObject(jsonData);
		return SUCCESS;
	}
	
	/**
	 * 更新管理组
	 * @return
	 * @throws Exception
	 */
	public String updateManager() throws Exception {
		Admin admin = (Admin) session.get("admin");
		purviewManager.setUpdateDate(DateUtil.getCurrentDate());
		purviewManager.setUpdateId(admin.getId());
		purviewManager.setUpdateName(admin.getUserName());
		purviewManagerService.update(purviewManager);
		result = JSONObject.fromObject(jsonData);
		return SUCCESS;
	}
	
	/**
	 * 删除管理组
	 * @return
	 * @throws Exception
	 */
	public String deleteManager() throws Exception {
		purviewManagerService.delete(purviewManager);
		result = JSONObject.fromObject(jsonData);
		return SUCCESS;
	}
	
	//common action
	
	@Override
	public String get() throws Exception {
		return null;
	}

	@Override
	public String query() throws Exception {
		List<String> menus = purviewGroupService.queryMenus(purviewManager.getId());
		if (menus != null && menus.size() > 0) {
			jsonData.put("menus", menus);
		}
		result = JSONObject.fromObject(jsonData);
		return SUCCESS;
	}

	@Override
	public String insert() throws Exception {
		Admin admin = (Admin) session.get("admin");
		PurviewGroup group = null;
		List<PurviewGroup> groups = new ArrayList<PurviewGroup>();
		if (purviewMenus != null && purviewMenus.length > 0) {
			for (String menuId : purviewMenus) {
				group = new PurviewGroup();
				group.setCreateDate(DateUtil.getCurrentDate());
				group.setCreateId(admin.getId());
				group.setCreateName(admin.getUserName());
				group.setPurviewManagerId(purviewManager.getId());
				group.setPurviewMenuId(menuId);
				groups.add(group);
			}
		}
		purviewGroupService.insertAll(purviewManager.getId(), groups);
		
		result = JSONObject.fromObject(jsonData);
		return SUCCESS;
	}

	@Override
	public String delete() throws Exception {
		return null;
	}

	@Override
	public String update() throws Exception {
		return null;
	}
	
	//getter setter
	
	public JSONObject getResult() {
		return result;
	}

	public void setResult(JSONObject result) {
		this.result = result;
	}

	public Map<String, Object> getRequest() {
		return request;
	}

	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public PurviewManager getPurviewManager() {
		return purviewManager;
	}

	public void setPurviewManager(PurviewManager purviewManager) {
		this.purviewManager = purviewManager;
	}

	public IPurviewGroupService getPurviewGroupService() {
		return purviewGroupService;
	}

	@Resource
	public void setPurviewGroupService(IPurviewGroupService purviewGroupService) {
		this.purviewGroupService = purviewGroupService;
	}

	public IPurviewManagerService getPurviewManagerService() {
		return purviewManagerService;
	}

	@Resource
	public void setPurviewManagerService(IPurviewManagerService purviewManagerService) {
		this.purviewManagerService = purviewManagerService;
	}

	public PurviewGroup getPurviewGroup() {
		return purviewGroup;
	}

	public void setPurviewGroup(PurviewGroup purviewGroup) {
		this.purviewGroup = purviewGroup;
	}

	public IPurviewColumnService getPurviewColumnService() {
		return purviewColumnService;
	}

	@Resource
	public void setPurviewColumnService(IPurviewColumnService purviewColumnService) {
		this.purviewColumnService = purviewColumnService;
	}

	public IPurviewMenuService getPurviewMenuService() {
		return purviewMenuService;
	}

	@Resource
	public void setPurviewMenuService(IPurviewMenuService purviewMenuService) {
		this.purviewMenuService = purviewMenuService;
	}

	public String[] getPurviewMenus() {
		return purviewMenus;
	}

	public void setPurviewMenus(String[] purviewMenus) {
		this.purviewMenus = purviewMenus;
	}
	
}
